package info.motteke.mybws.script;

public class Scripter {

    public String parse(String expression, ValueStack stack) {
        StringBuilder text = new StringBuilder();
        StringBuilder buf = null;
        
        boolean inScript = false;
        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);

            if (inScript) {
                if (c == '`') {
                    text.append(stack.eval(buf.toString()));
                    buf = null;
                    inScript = false;
                } else {
                    buf.append(c);
                }
            } else {
                if (c == '`') {
                    buf = new StringBuilder();
                    inScript = true;
                } else {
                    text.append(c);
                }
            }
        }
        
        if (inScript) {
            throw new IllegalArgumentException(expression);
        }

        return text.toString();
    }
}
